import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Menu from '../components/home/Menu.vue'
import Order from '../components/home/Order.vue'
import Account from '../components/home/Account.vue'
import Dishes from '../components/home/Dishes.vue'
import Analyse from '../components/home/Analyse.vue'

Vue.use(VueRouter)
// 路由规则
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home', component: Home, redirect: '/order', children: [// 默认订单页面
      { path: '/order', component: Order },
      { path: '/menu', component: Menu },
      { path: '/account', component: Account },
      { path: '/dishes', component: Dishes },
      { path: '/analyse', component: Analyse },
  ]}
]

// 抛出路由
const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, form, next) => {
  // 如果没有token访问系统内部页面，则直接跳转至登录页面
  if(to.path == '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
